using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using BoOnce.Components;
using BoOnce.Physic;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;

namespace BoOnce.GameLogic.Entities
{
    abstract class PhysicalScenery : Scenery, IPhysical
    {
        #region IPhysical Attributes
        private Ground _Ground;
        private float _Height;
        private float _Width;
        private float _Mass;
        private Rectangle _BoundingBox;

        protected PhysicComponent _PhysicComponent;
        protected Body _Body;
        protected Geom _Geom;
        #endregion

        #region Properties
        public Boolean IsStatic
        {
            get { return _Body.IsStatic; }
            set { _Body.IsStatic = value; }
        }
        #endregion

        #region Constructor
        public PhysicalScenery(Vector2 position, float width, float height, float mass)
        {
            _Height = height;
            _Width = width;
            _Mass = mass;
            _BoundingBox = new Rectangle((int)position.X, (int)position.Y, (int)width, (int)height);
            InitPhysics(position);
        }
        #endregion

        #region IPhysical
        private void InitPhysics(Vector2 position)
        {
            _PhysicComponent = PhysicComponent.Instance;
            _Body = BodyFactory.Instance.CreateRectangleBody(_Width, _Height, _Mass);
            _Geom = GeomFactory.Instance.CreateRectangleGeom(_Body, _Width, _Height);
            Position = position;
            EnablePhysical(true);
            _Ground = _PhysicComponent.AddPhysicalEntity(_Body, _Geom, _Height);
        }

        public Body Body { get { return _Body; } }

        public Geom Geom { get { return _Geom; } }

        public Rectangle BoundingBox { get { return _BoundingBox; } }

        public bool Intersect(IPhysical physic)
        {
            return _BoundingBox.Intersects(physic.BoundingBox);
        }

        public bool Intersect(Rectangle rect)
        {
            return _BoundingBox.Intersects(rect);
        }

        public void EnablePhysical(bool activated)
        {
            _Body.Enabled = activated;
            _Geom.CollisionEnabled = activated;
        }

        public Vector2 Position
        {
            get
            {
                return _Body.Position;
            }
            set
            {
                _Body.Position = value;
                _BoundingBox.X = (int)(_Body.Position.X - _Width / 2.0f);
                _BoundingBox.Y = (int)(_Body.Position.Y - _Height / 2.0f);
            }
        }

        public Vector2 LinearVelocity
        {
            get { return _Body.LinearVelocity; }
        }

        public float Mass
        {
            get
            {
                return _Body.Mass;
            }
            set
            {
                _Body.Mass = value;
            }
        }

        public float Width
        {
            get { return _Width; }
        }

        public float Height
        {
            get { return _Height; }
        }
        #endregion
    }
}
